﻿using System;
using System.IO;

namespace Agehack
{
    public static class TrafficLogger
    {
        // Set to [false] to disable traffic tracing.
        public static bool Enabled = true;
        public static Stream LogStream = null;
        public static void Log(bool received, Flap f)
        {
            if (!Enabled)
                return;

            string sender = "me>>";
            if (received)
                sender = "me<<";

            if (LogStream != null)
            {
                LogStream.Write(new byte[] { Convert.ToByte(received) }, 0, 1);
                byte[] buf = f.GetBytes();
                LogStream.Write(buf, 0, buf.Length);
                LogStream.Flush();
            }
            else
            {
                Console.Write(sender + ": " + f.ToString() + " \t");
                string str = ToHex(f.GetBytes()).Replace("-", " ");
                Console.WriteLine(str);
            }
        }

        /// <summary>
        /// Converts byte array to a hexadecimal string.
        /// </summary>
        /// <param name="barr">Array to convert</param>
        /// <returns>HEX-code string</returns>
        public static string ToHex(byte[] barr)
        {
            return BitConverter.ToString(barr);

            /*string hex="";                        // THIS IS SHIT !
            foreach (byte b in barr)
                hex += String.Format("{0:X}", b);
            return hex;*/
        }
    }
}
